const pubUrl = "http://jswxedu.com/applet/"//这是我要请求的数据接口的公共部分
const wxRequest =function(options){
  var openid=wx.getStorageSync('openid');
  return new Promise((resolve,reject) => {
    wx.request({
      url: pubUrl+options.url+"?openid="+openid,
      method: 'post',
      data:options.data || {},
      header: {
        'content-type':'application/x-www-form-urlencoded',
      },
      success:function(res){
        if(res.data.code==-3){
          wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              wx.request({
                url: 'http://jswxedu.com/applet/auth/wxLogin',
                data:{code:res.code},
                success:function(result){
                  wx.setStorageSync('openid', result.data.openid);
                  wxRequest(options);
                }
              })
            }
          })
        }
        resolve(res)
      },
      fail: function(error){
        reject(error)
      }
    })
  })
}
export default wxRequest